﻿namespace net_ddns_server_ali
{
    class AliyunDNS
    {
        #region 字段
        private readonly Aliyun.Acs.Core.IAcsClient _client;
        private ILog _log;
        #endregion

        #region 属性
        public ILog Log
        {
            get { return _log; }
            set { _log = value; }
        }
        #endregion

        #region 构造函数
        public AliyunDNS(string accessKeyId, string accessKeySecret) : this(AliyunCore.CreateClient("cn-hangzhou", accessKeyId, accessKeySecret))
        {
        }
        public AliyunDNS(Aliyun.Acs.Core.IAcsClient client)
        {
            _client = client;
        }
        #endregion

        #region 动作
        public bool AddA(string domain, string type, string name, string ip, int ttl = 600)
        {
            var request = new Aliyun.Acs.Alidns.Model.V20150109.AddDomainRecordRequest()
            {
                RR = name,
                Type = type,
                _Value = ip,
                TTL = ttl,
                Priority = 1,
                DomainName = domain,
            };
            var response = AddDomainRecord(request);
            return response != null && response.HttpResponse.Status == 200;
        }
        public bool ModifyA(string domain,string type, string name, string ip)
        {
            if (string.IsNullOrEmpty(domain) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(ip))
                return false;
            var records = DescribeDomainRecords(new Aliyun.Acs.Alidns.Model.V20150109.DescribeDomainRecordsRequest()
            {
                DomainName = domain,
            });
            if (records == null || records.DomainRecords.Count == 0)
            {
                return false;
            }
            var item = records.DomainRecords.Find(p => string.Equals(p.RR, name, StringComparison.OrdinalIgnoreCase));
            if (item == null)
                return false;
            if (string.Equals(item._Value, ip, StringComparison.OrdinalIgnoreCase))
                return true;
            var request = new Aliyun.Acs.Alidns.Model.V20150109.UpdateDomainRecordRequest()
            {
                RecordId = item.RecordId,
                RR = item.RR,
                Type = type,
                _Value = ip,
                TTL = item.TTL,
                Priority = item.Priority,
                Line = item.Line,
            };
            if (request.Priority == null)
                request.Priority = 1;
            var response = UpdateDomainRecord(request);
            Log?.Info($"\t\t=>{response?.HttpResponse.Status}   recordId={response?.RecordId}");
            return response != null && response.HttpResponse.Status == 200;
        }
        #endregion

        #region 阿里云接口
        public Aliyun.Acs.Alidns.Model.V20150109.DescribeDomainRecordsResponse DescribeDomainRecords(Aliyun.Acs.Alidns.Model.V20150109.DescribeDomainRecordsRequest request)
        {
            try
            {
                return _client.GetAcsResponse(request);
            }
            catch (Exception error)
            {
                Log?.Error(error);
                return null;
            }
        }
        public Aliyun.Acs.Alidns.Model.V20150109.DeleteDomainRecordResponse DeleteDomainRecord(Aliyun.Acs.Alidns.Model.V20150109.DeleteDomainRecordRequest request)
        {
            try
            {
                return _client.GetAcsResponse(request);
            }
            catch (Exception error)
            {
                Log?.Error(error);
                return null;
            }
        }
        public Aliyun.Acs.Alidns.Model.V20150109.AddDomainRecordResponse AddDomainRecord(Aliyun.Acs.Alidns.Model.V20150109.AddDomainRecordRequest request)
        {
            try
            {
                return _client.GetAcsResponse(request);
            }
            catch (Exception error)
            {
                Log?.Error(error);
                return null;
            }
        }
        public Aliyun.Acs.Alidns.Model.V20150109.UpdateDomainRecordResponse UpdateDomainRecord(Aliyun.Acs.Alidns.Model.V20150109.UpdateDomainRecordRequest request)
        {
            try
            {
                return _client.GetAcsResponse(request);
            }
            catch (Exception error)
            {
                Log?.Error(error);
                return null;
            }
        }
        #endregion

    }
}
